#include "uart_drv.h"
#include "modbus_user.h"

static RingBuffer_t queue1              = {0};
static RingBuffer_t queue2              = {0};
static uint8_t xdata u1buff_srouce[256] = {0};
static uint8_t xdata u2buff_srouce[256] = {0};

UARTx_t uart1 = {0};
UARTx_t uart2 = {0};

void Uart1SendByte(uint8_t *buf, uint16_t len)
{
    uint16_t i = 0;
    for(; i < len; ++i)
        TX1_write2buff(buf[i]);
}

void Uart2SendByte(uint8_t *buf, uint16_t len)
{
    uint16_t i = 0;
    for(; i < len; ++i)
        TX2_write2buff(buf[i]);
}

void UART_Init(void)
{
    // Hardware Config
    COMx_InitDefine uartCnf;
    uartCnf.UART_Mode       = UART_8bit_BRTx;
    uartCnf.UART_BRT_Use    = BRT_Timer1;
    uartCnf.UART_BaudRate   = 9600ul;
    uartCnf.Morecommunicate = DISABLE;
    uartCnf.UART_RxEnable   = ENABLE;
    uartCnf.BaudRateDouble  = DISABLE;
    UART_Configuration(UART1, &uartCnf);
    NVIC_UART1_Init(ENABLE,Priority_1);

    uartCnf.UART_BRT_Use    = BRT_Timer2;
    uartCnf.UART_BaudRate   = 9600ul;
    UART_Configuration(UART2, &uartCnf);
    NVIC_UART2_Init(ENABLE,Priority_0);

    P1_MODE_OUT_PP(GPIO_Pin_1);
    P1_MODE_IO_PU(GPIO_Pin_0);
	UART1_SW(UART1_SW_P30_P31);	
    UART2_SW(UART2_SW_P10_P11);

    // Software Config
    Queue_Init(&queue1, u1buff_srouce, 256);
    Queue_Init(&queue2, u2buff_srouce, 256);
    uart1.send      = Uart1SendByte;
    uart1.queue     = &queue1;
    uart2.send      = Uart2SendByte;
    uart2.queue     = &queue2;
    ModbusInit();
}